<template>
  <div>
    <!-- 面包屑头部区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>代理商管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加代理商</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片内容区域 -->
    <el-card class="box-card">
      <!-- 搜索框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addVisible = true">添加代理商</el-button>
        </el-col>
      </el-row>

      <!-- 表格显示 -->
      <el-table :data="tableData" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="电话" prop="phone"></el-table-column>
        <el-table-column label="角色" prop="role"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.code" active-color="#13ce66" inactive-color="#eee"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改用户" placement="top" :enterable="false">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="showVisible(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除用户" placement="top" :enterable="false">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteUser(scope.row.id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="分配屏幕" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      ></el-pagination>
    </el-card>

    <!-- 弹出添加代理商对话框 -->
    <el-dialog :visible.sync="addVisible" width="50%" title="添加代理商" @close="addFormClose">
      <!-- 内容区域 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
        <el-form-item label="用户名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserInfo">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 弹出修改代理商对话框 -->
    <el-dialog title="修改代理商" :visible.sync="editVisible" width="50%">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
        <el-form-item label="用户名">
          <el-input v-model="editForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    // 自定义 校验规则
    var checkPhone = (rule, value, cb) => {
      const regPhone = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/
      if (regPhone.test(value)) return cb()
      cb(new Error('请输入合法的手机号'))
    }
    return {
      // 自定义模拟用户列表
      tableData: [
        {
          name: '张三',
          email: '123@qq.com',
          phone: '13128828001',
          role: '超级管理员',
          code: true
        },
        {
          name: '测试人员1',
          email: '123@qq.com',
          phone: '13128828001',
          role: '会员',
          code: true
        },
        {
          name: '测试人员3',
          email: '123@qq.com',
          phone: '13128828001',
          role: '普通用户',
          code: false
        }
      ],
      // 自定义模拟请求数据参数
      queryInfo: {
        query: '',
        // 当前页数
        pageNum: 1,
        // 当前显示多少条数据
        pageSize: 1
      },
      // 总共人数
      total: 10,
      // 显示或隐藏 添加用户对话框
      addVisible: false,
      // 显示或隐藏 修改用户对话框
      editVisible: false,
      // 用户信息
      addForm: {
        name: '',
        password: '',
        phone: '',
        email: ''
      },
      // 校验规则
      addFormRules: {
        name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入合法的邮箱',
            trigger: ['blur', 'change']
          }
        ]
      },
      // 用户信息
      editForm: {
        name: '',
        password: '',
        phone: '',
        email: ''
      },
      // 校验规则
      editFormRules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入合法的邮箱',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    // 每页显示多少条数据
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryInfo.pageSize = val
    },
    // 当前页面码数
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryInfo.pageNum = val
    },
    // 关闭对话框清空表单数据
    addFormClose() {
      this.$refs.addFormRef.resetFields()
    },
    // 点击确定添加用户
    addUserInfo() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        // 向用户发起请求添加数据
        this.$message.success('添加用户成功')
        this.addVisible = false
      })
    },
    // 点击修改按钮显示对话框
    showVisible(res) {
      console.log(res)
      this.editVisible = true
      this.editForm = res
    },
    // 点击删除用户
    async deleteUser(id) {
      let test = '此操作将永久删除该用户, 是否继续?'
      const res = await this.$confirm(test, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (res !== 'confirm') return this.$message.info('已取消删除')
      this.$message.success('删除成功')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
